package com.hundsun.cprs.yyt.web.action.account;

import com.hundsun.cprs.yyt.service.account.enums.EnumQuotaTransType;
import com.hundsun.cprs.yyt.service.quota.service.QuotaService;
import com.hundsun.cprs.yyt.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hundsun.cprs.yyt.service.account.domain.query.AccountQuotaLogQuery;
import com.hundsun.cprs.yyt.service.account.service.AccountQuotaLogService;
import com.hundsun.cprs.yyt.web.action.BaseAction;
import com.hundsun.network.eclp.ex.common.PermissionEnum;
import com.hundsun.network.eclp.ex.security.AdminAccess;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Controller
@RequestMapping("/account/quota")
public class AccountQuotaLogAction extends BaseAction {
    
    @Autowired
    private AccountQuotaLogService accountQuotaLogService;
    @Autowired
    private QuotaService quotaService;
    
    @AdminAccess(PermissionEnum.ACCOUNT_QUOTA_LIST)
    @RequestMapping("/list")
    public void list(@ModelAttribute("query") AccountQuotaLogQuery query, ModelMap model){
        if (query.getCreateDateL() == null){
            query.setCreateDateL(DateUtil.getBeginOfDay(new Date()));
        }
        if (query.getCreateDateR() == null){
            query.setCreateDateR(DateUtil.getEndOfDay(new Date()));
        }
        accountQuotaLogService.selectByPage(query);
        Map<String, EnumQuotaTransType> enumQuotaTransTypeMap = EnumQuotaTransType.toMap();
        //暂时排除手工冻结和积分兑换类型的展示
        enumQuotaTransTypeMap.remove(EnumQuotaTransType.TRANS_0103.getCode());
        enumQuotaTransTypeMap.remove(EnumQuotaTransType.TRANS_0302.getCode());
        model.addAttribute("EnumQuotaTransTypeMap",enumQuotaTransTypeMap);
        model.addAttribute("quotaList", quotaService.queryAll());
    }

    @Override
    protected void initBinder(WebDataBinder binder) {
        // 注册默认的日期格式化类型: yyyy-MM-dd
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }
}
